import Pages from '@/routes/Pages'
import Meta from '@/components/Meta'
import theme from '@/theme'
import zhCN from 'antd/locale/zh_CN'
import baseRoutes from '@/routes'
import { ConfigProvider } from 'antd'
import { getCurrentRouterMap, RouterBeforeEach } from '@/routes/permission.ts'
import { useAppSelector } from '@/hooks/redux.ts'
import { useLocation } from 'react-router-dom'
import { useEffect, useState } from 'react'
import { Routes } from '@/routes/types.ts'

function Boot() {
  const location = useLocation()
  const themeSetting = useAppSelector('themeSetting')
  const userInfo = useAppSelector('userInfo')
  const [route, setRoute] = useState<Routes | null>()
  useEffect(() => {
    const router = getCurrentRouterMap([...baseRoutes, ...userInfo.authMenu], location.pathname)
    setRoute(router)
  }, [location])

  return (
    <ConfigProvider locale={zhCN} theme={theme[themeSetting.mode]}>
      <RouterBeforeEach>
        <Meta title={route?.title} />
        <Pages />
      </RouterBeforeEach>
    </ConfigProvider>
  )
}

export default Boot
